package com.zcc.makingfriend.service.impl;

import com.zcc.makingfriend.dao.Comment;
import com.zcc.makingfriend.dao.Commented;
import com.zcc.makingfriend.dao.User;
import com.zcc.makingfriend.dto.CommentDto;
import com.zcc.makingfriend.dto.CommentedDto;
import com.zcc.makingfriend.mapper.CommentMapper;
import com.zcc.makingfriend.mapper.CommentedMapper;
import com.zcc.makingfriend.mapper.UserMapper;
import com.zcc.makingfriend.service.CommentService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zcc
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    CommentedMapper commentedMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public int addComment(Comment comment) {
        comment.setLikes(0L);
        comment.setStatus(0);
        comment.setCreated(LocalDateTime.now());
        comment.setCommented(0);
        return commentMapper.addComment(comment);
    }

    @Override
    public int deleteComment(Long id) {
        return commentMapper.deleteComment(id);
    }

    @Override
    public int isLikeComment(Long id, int isLike, Long likes) {
        if (isLike == 1) {
            likes += 1;
        } else {
            likes -= 1;
        }
        return commentMapper.isLikeComment(id, likes);
    }

    @Override
    public int reportComment(Long id) {
        // 还需要处理举报后推送给谁的逻辑
        int i = commentMapper.reportComment(id);
        return i;
    }

    @Override
    public List<CommentDto> findCommentsByArticleId(Long articleId) {
        List<CommentDto> commentDtoList = new ArrayList<>();
        List<Comment> commentsByArticleId = commentMapper.findCommentsByArticleId(articleId);
        for (Comment comment: commentsByArticleId) {
            CommentDto commentDto = new CommentDto();
            Long authorId = comment.getAuthorId();
            User author = userMapper.findUserById(authorId);
            commentDto.setComment(comment);
            commentDto.setAuthorId(authorId);
            commentDto.setAvatar(author.getAvatar());
            commentDto.setUsername(author.getUsername());
            List<Commented> commentedByCommentId = commentedMapper.findCommentedByCommentId(comment.getId());
            List<CommentedDto> commentedDtos = new ArrayList<>();
            for (Commented commented: commentedByCommentId) {
                Long userId = commented.getAuthorId();
                User user = userMapper.findUserById(userId);
                CommentedDto commentedDto = new CommentedDto();
                commentedDto.setCommented(commented);
                commentedDto.setAvatar(user.getAvatar());
                commentedDto.setAuthorId(user.getId());
                commentedDto.setUsername(user.getUsername());
                commentedDtos.add(commentedDto);
            }
            commentDto.setCommentedList(commentedDtos);
            commentDtoList.add(commentDto);
        }
        return commentDtoList;
    }

}
